<?php

use app\models\Mall;
use yii\helpers\Url;

$web = Url::base();

//单位
$integral_unit = Mall::getCurrentInstitutionIntegralUnit();
//比率
$integral_rate = Mall::getCurrentInstitutionIntegralRate();
//是否可以混合支付
$is_cash_order = Mall::getCurrentInstitutionIsCashOrder();
$site_name = Mall::getCurrentInstitutionSiteName();
$institution_code = Mall::getCurrentInstitutionCode();
$this->title = $site_name . '-订单详情';

//是否有跨境电商商品
$is_crossborder = false;
foreach ($orderGoodsList as $item) {
    if ($item['is_crossborder'] == '1') {
        $is_crossborder = true;
        break;
    }
}
?>

    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/order-detail.css"/>
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/pay-window.css"/>
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/await-pay.css"/>
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/cross-title.css"/>
    <style type="text/css">
        .reminder {
            overflow: hidden;
        }

        .reminder .reminder-name {
            margin-top: .5rem;
            font-size: .6rem;
            color: #e50038;
        }

        .reminder .reminder-text {
            margin-top: .4rem;
            font-size: .6rem;
            color: #898989;
        }

        .reminder .reminder-text span {
            color: #e50038;
        }
    </style>
    <div class="main">
        <div class="order-state">
            <p><?= $orderInfo['orderStatusTxt'] ?></p>
        </div>
<!--        <div class="reminder">-->
<!--            <p class="reminder-name">温馨提示：</p>-->
<!--            <p class="reminder-text">由于物流放假,春节期间所兑换的产品均年后发货,放假时间为<span>2017年1月18日 至 2017年2月5日</span>。给您带来的不便敬请谅解。</p>-->
<!--        </div>-->
        <?php
        if ($is_crossborder == true) {
            ?>
<!--            <div class="reminder">-->
<!--                <p class="reminder-name">温馨提示：</p>-->
<!--                <p class="reminder-text">由于海关系统升级于<span>16年12月30日18:00</span>后兑换的跨境产品，需要<span>17年1月10日</span>开始发货，给您带来的不变敬请谅解。</p>-->
<!--            </div>-->
            <?php
        }
        ?>

        <div class="detail">
            <div class="logistics">
                <?php
                foreach ($orderDeliveryList as $item) {
                    ?>
                    <a href="delivery-detail?delivery_id=<?= $item['id'] ?>">
                        <div class="logistics-message">
                            <img class="logistics-icon f-l" src="<?= $web ?>/images/mall-m/logistics-icon.png"/>
                            <div class="information f-l">
                                <p>快件已经发货/物流公司：<span><?= $item['logistics_company'] ?></span></p>
                                <p>运单编号：<span><?= $item['logistics_code'] ?></span></p>
                                <p><span><?= date('Y-m-d', strtotime($item['create_datetime'])) ?></span><span class="time"><?= date('h:i:s', strtotime($item['create_datetime'])) ?></span></p>
                            </div>
                            <img class="a-icon f-r" src="<?= $web ?>/images/mall-m/a-icon.png"/>
                        </div>
                    </a>
                    <?php
                }
                ?>

                <!--<a href="">
                <div class="logistics-message">
                    <img class="logistics-icon f-l" src="<? /*= $web */ ?>/images/mall-m/logistics-icon.png"/>
                    <div class="information f-l">
                        <p>快件已经发货/物流公司：<span>顺丰速运</span></p>
                        <p>运单编号：<span>888888888477</span></p>
                        <p><span>2016-09-20</span><span class="time">16:39:00</span></p>
                    </div>
                    <img class="a-icon f-r" src="<? /*= $web */ ?>/images/mall-m/a-icon.png"/>
                </div>
            </a>-->
            </div>
            <div class="address">
                <img class="f-l" src="<?= $web ?>/images/mall-m/location-icon.png"/>
                <div class="user f-l">
                    <p class="user-name">收货人：<span><?= $orderInfo['receiver_name'] ?></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><?= $orderInfo['receiver_phone'] ?></span></p>
                    <p>收货地址：<span><?= $orderInfo['receiver_province'] ?><?= $orderInfo['receiver_city'] ?><?= $orderInfo['receiver_region'] ?><?= $orderInfo['receiver_address'] ?></span></p>
                </div>
            </div>
        </div>
        <div class="order">
            <?php
            $sum_integral = 0;
            foreach ($orderGoodsList as $item) {
                $sum_integral = $sum_integral + ($item['selling_integral'] * $item['quantity']);
                ?>
                <div class="order-detail">
                    <div class="content">
                        <a href="goods-detail?goods_code=<?= $item['goods_code'] ?>&institution_code=<?= $institution_code ?>">

                            <img class="f-l" src="<?= $item['quantity'] == 0 ? $web . '/images/mall-m/goods-off-shelves.png' : $item['image_url'] . '@120w' ?>"/>
                        </a>
                        <div class="f-r">
                            <p class="title crossborder">
                                <?php
                                if ($item['is_crossborder'] == '1') {
                                    ?>
                                    <span>跨境</span>
                                    <?php
                                }
                                ?>
                                <?= $item['goods_name'] ?></p>
                            <p class="classify">商品型号：<span><?= $item['model'] ?></span></p>
                            <p class="size">商品规格：<span><?= $item['format'] ?></span></p>
                            <p class="size f-l">数量：<span><?= $item['quantity'] ?></span></p>
                            <p class="integral f-r">单价：<span><?= intval($item['selling_integral']) ?><?= $integral_unit ?></span></p>
                        </div>

                        <?php
                        if ($orderInfo['status'] == '1') {
                            ?>
                            <p class="classify f-l">库存：<span><?= $item['stock'] ?></span></p>
                            <?php
                        }
                        ?>
                    </div>
                </div>
                <?php
            }
            ?>
            <!-- <div class="order-detail">
            <div class="content">
                <a href="">
                    <img class="f-l" src="<? /*= $web */ ?>/images/mall-m/list-content-img.jpg"/>
                </a>
                <p class="title f-l">呆萌抱抱大肚圣诞陶瓷杯大肚圣诞老人圣诞老人</p>
                <p class="integral f-r">积分：<span>1198</span></p>
                <p class="classify f-l">颜色分类：<span>黄/红/绿</span></p>
                <p class="size f-l">尺寸：<span>12x32</span></p>
            </div>
        </div>-->

        </div>


        <div class="calculate">
            <?php
            if ($orderInfo['status'] == '1') {
                ?>
                <div class="price">
                    <p class="commodity"><span class="f-l">商品总价</span><span class="f-r"><?= $integral_unit ?>：<span><?= $sum_integral ?></span></span></p>
                    <p class="freight"><span class="f-l">运费（快递)</span><span class="f-r">¥<span>0.00</span></span></p>


                    <?php
                    $pay_integral = $sum_integral;
                    if ($is_cash_order == '1' && $pay_integral > $userIntegral) {
                        $pay_integral = $userIntegral;
                        $pay_cash = round(($sum_integral - $pay_integral) / $integral_rate, 2);
                        ?>
                        <p class="total"><span class="f-l">订单总价</span><span class="f-r"><?= $integral_unit ?>:<span><?= $pay_integral ?></span></span></p>
                        <p class="total"><span class="f-l"></span><span class="f-r">现金:<span><?= $pay_cash ?>￥</span></span></p>
                        <?php
                    } else {
                        ?>
                        <p class="total"><span class="f-l">订单总价</span><span class="f-r"><?= $integral_unit ?>:<span><?= $pay_integral ?></span></span></p>
                        <?php
                    }
                    ?>

                </div>
                <div class="payment">
                    <p><span class="f-l">剩余：</span><span class="pay-integral f-r"><?= $integral_unit ?>:<span><?= intval($userIntegral) ?></span></span></p>
                </div>

                <?php

            } else {
                $sum_pay_integral = $orderInfo['integral'];
                if ($orderInfo['cash_total_fee']) {
                    $sum_pay_integral = $sum_pay_integral - $orderInfo['cash_integral'];
                }
                ?>
                <div class="price">
                    <p class="commodity"><span class="f-l">商品总价</span><span class="f-r"><?= $integral_unit ?>：<span><?= intval($orderInfo['integral']) ?></span></span></p>
                    <p class="freight"><span class="f-l">运费（快递)</span><span class="f-r">¥<span>0.00</span></span></p>
                    <p class="total"><span class="f-l">订单总价</span><span class="f-r"><?= $integral_unit ?>:<span><?= intval($orderInfo['integral']) ?></span></span></p>
                </div>

                <div class="payment">
                    <p><span class="f-l">实付款</span><span class="pay-integral f-r"><?= $integral_unit ?>:<span><?= intval($sum_pay_integral) ?></span></span></p>
                </div>
                <?php
                $pay_cash = 0;
                if ($orderInfo['cash_total_fee']) {
                    $pay_cash = $orderInfo['cash_total_fee'] / 100;
                    ?>
                    <div class="payment">
                        <p><span class="f-l"></span><span class="pay-integral f-r">现金:￥<span><?= $pay_cash ?></span></span></p>
                    </div>
                    <?php
                }

            }
            ?>

        </div>
        <a href="tel:400--860--3111">
            <div class="tell-phong">
                <img class="f-l" src="<?= $web ?>/images/mall-m/call-icon.png"/>
                <p class="f-l">拨打电话</p>
            </div>
        </a>
        <div class="message">
            <p>订单编号：<span><?= $orderInfo['code'] ?></span></p>
            <p>创建时间：<span><?= $orderInfo['create_datetime'] ?></span></p>
            <p>付款时间：<span><?= $orderInfo['modify_datetime'] ?></span></p>
        </div>
        <?php
        if ($orderInfo['status'] == '1') {
        if ($all_stock_status == '1') {
            ?>
            <a class="affirm-btn" href="javascript:payExec()">订单支付</a>
            <?php
        }else{
            ?>
            <a class="affirm-btn-1" href="javascript:void(0)">没有库存</a>
        <?php
        }
        ?>
            <a class="affirm-btn-2 " href="javascript:cancelOrder()">取消订单</a>
            <script>
                cancelOrder = function () {
                    if (confirm("是否确认取消订单?")) {
                        var data = {};
                        data.order_code = "<?=$orderInfo['code']?>";
                        data.remark = '用户取消订单';
                        var re = requestJsonMallM('cancel-order-handle', data);
                        if (re.status == 0) {
                            alert(re.message);
                            window.location.reload();
                        } else {
                            alert(re.message);
                        }
                    }
                };
            </script>
            <?php
        } else {
            ?>
            <!-- <a class="affirm-btn"> 确认收货 </a>-->
            <?php
        }
        ?>
        <!-- -->
    </div>
    <script>
        var is_cash_order =<?=$is_cash_order?>;
        var cash_integral = 0;

        payExec = function () {
            if (confirm("是否确认支付订单?")) {
                $('.await-window').show();
                var data = {};
                data.orderId = "<?=$orderInfo['id']?>";
                var re = requestJsonMallM('pay-order-handle', data, true, function (re) {
                    if (re.status == 0) {
                        var payOrderResult = re.data.payOrderResult;
                        if (payOrderResult.status == '0') {
                            // alert('生成订单成功');
                            window.location.href = "<?=Url::to(['mall-m/pay-order-success', 'institution_code' => Mall::getCurrentInstitutionCode()])?>" + '&orderId=' + payOrderResult.orderId;
                        } else {
                            if (payOrderResult.status == '20' && is_cash_order == '1') {
                                //提示是否开始混合支付
                                cash_integral = payOrderResult.cash_integral;
                                $('.await-window').hide();
                                $('.popup-window').show();
                                return;
                            }
                            alert(payOrderResult.message);
                        }

                    } else {
                        alert(re.message);
                    }
                    $('.await-window').hide();
                });

            }

        };

        payMix = function () {
            $('.popup-window').hide();
            //开启混合支付流程
            var orderCode = "<?=$orderInfo['code']?>";
            if (is_weixin()) {
                invoke_weixinPay(cash_integral, orderCode);
            } else {
                $('#alipayment [name="integral"]').val(cash_integral);
                $('#alipayment [name="order_code"]').val(orderCode);
                $('#alipayment').submit();
            }
        };

        //微信支付入口
        var jsApiParameters = {};
        var out_trade_no = '';
        var v_orderCode = '';
        function invoke_weixinPay(integral, orderCode) {
            var data = {};
            data.integral = integral;
            data.orderCode = orderCode;
            var re = requestJsonMallM("<?=Url::to(['pay-integral/mall-m-weixin-pay-handle'])?>", data);
            if (re.status == 0) {
                jsApiParameters = re.data.weixinpayJs;
                out_trade_no = re.data.out_trade_no;
                v_orderCode = orderCode;
            } else {
                alert(re.message);
                window.location.reload(true);
                return;
            }

            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
        }


        //微信支付
        onBridgeReady = function () {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                jsApiParameters,
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        window.location.href = "<?=Url::to(['mall-m/pay-order-success', 'institution_code' => Mall::getCurrentInstitutionCode()])?>" + '&isMix=1&out_trade_no=' + out_trade_no;
                    } else {
                        if (res.err_msg == "get_brand_wcpay_request:cancel") {
                            alert("支付取消");
                        } else {
//                        alert(JSON.stringify(res));
                            if (res.err_code == '3') {//扫码支付
                                showWxPayCode(cash_integral, v_orderCode);
                                return;
                            } else {
                                alert("error支付错误:" + res.err_desc);
                            }
                        }
                        window.location.href = "<?=Url::to(['mall-m/order-list', 'institution_code' => Mall::getCurrentInstitutionCode()])?>";
                    }
                }
            );
        };

    </script>
    <form id="alipayment" action='<?= Url::to(["pay-integral/mall-alipay-pay"]) ?>' method='post' style="display: none;">
        支付宝支付
        积分:<input name="integral" type="input"/>
        订单号:<input name="order_code" type="input"/>
        <input type="submit" value="支付宝支付" id="btn_ali">
    </form>
    <div class="await-window" style="display: none;">
        <div class="window-content">
            <img src="<?= $web ?>/images/mall-m/await-ing.png"/>
            <p>等待支付完成</p>
        </div>
        <div class="shade"></div>
    </div>
    <div class="popup-window" style="display: none;">
        <div class="popup">
            <div class="popup-heade">
                <p>温馨提醒</p>
                <a href="" class="close-btn">
                    <img src="<?= $web ?>/images/mall-m/pay-window-close.png"/>
                </a>
            </div>
            <div class="popup-content">
                <p class="popup-money">您需要支付现金:<span>￥<?= isset($pay_cash) ? $pay_cash : 0 ?></span></p>
                <p>当您取消订单或退货时，支付的现金部分将以积分形式退还到您的帐户。</p>
            </div>
            <div class="boundary"></div>
            <div class="btn">
                <a href="javascript:$('.popup-window').hide();" class="deny-btn">不，我再想想</a>
                <a href="javascript:payMix();" class="confirm-btn">行，我知道了</a>
            </div>
        </div>
        <div class="shade"></div>
    </div>

    <!--扫码支付-->
<?= \app\widgets\MallMWxNativePay::widget([]) ?>